package com.youka.bussiness.controller.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import com.youka.bussiness.common.bean.RestResp;
import com.youka.bussiness.common.base.BaseEntity;
import com.youka.bussiness.common.base.BaseController;
import com.youka.bussiness.service.GoodsService;
import com.youka.bussiness.service.req.GoodsReqDto;
import com.youka.bussiness.service.req.GoodsUpdateDto;
import com.youka.bussiness.service.req.GoodsPageDto;
import com.youka.bussiness.service.res.GoodsDto;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.ArrayList;
import java.util.List;


import java.util.Map;

/**
 * 商品 Controller
 *
 * @author lfc
 * @date 2024-06-30 16:47:29
 */
@Api(tags = "web-商品")
@RestController()
@RequestMapping("/web/Goods")
public class GoodsController extends BaseController{

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询商品详情
     */
    @ApiOperation(value = "获取商品详情")
    @PostMapping(value = "/single")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询商品详情", response = GoodsDto.class)
    })
    public RestResp findGoodsById(@RequestBody BaseEntity entity) {
        GoodsDto dto = goodsService.selectById(entity.getId());
        return success(dto);
    }

    /**
     * 查询商品集合
     */
    @ApiOperation(value = "查询商品集合")
    @PostMapping("/list")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询商品详情", response = GoodsDto.class)
    })
    public RestResp listGoods(@RequestBody GoodsReqDto goodsPageDto) {
        List<GoodsDto> list = goodsService.selectByList(goodsPageDto);
        return success().toAdminTree(list);
    }

    /**
     * 查询商品分页
     */
    @ApiOperation(value = "查询商品分页")
    @PostMapping("/page")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询商品详情", response = GoodsDto.class)
    })
    public RestResp pageGoods(@RequestBody GoodsPageDto goodsPageDto) {
        IPage<GoodsDto> page = goodsService.selectByPage(goodsPageDto);
        return success().toAdminPage(page);
    }

    /**
     * 新增商品
     */
    @ApiOperation(value = "新增商品")
    @PostMapping("/add")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询商品详情", response = RestResp.class)
    })
    public RestResp addGoods(@RequestBody GoodsReqDto goodsReqDto) {
        goodsService.insert(goodsReqDto);
        return success();
    }

    /**
     * 编辑商品
     */
    @ApiOperation(value = "编辑商品")
    @PostMapping("/update")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询商品详情", response = RestResp.class)
    })
    public RestResp updateGoods(@RequestBody GoodsUpdateDto goodsReqDto) {
        goodsService.update(goodsReqDto);
        return success();
    }

    /**
     * 删除商品主键
     */
    @ApiOperation(value = "删除商品")
    @PostMapping("/delete")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询商品详情", response = RestResp.class)
    })
    public RestResp delete(@RequestBody BaseEntity entity) {
        goodsService.deleteById(entity.getId());
        return success();
    }

}
